<?php

class Db_Tables2_BDictMenu
{	
	var $user_group;
	var $shop_id;
	
	function __construct() {
		$this->dealers_db = Zend_Registry::get("dealers_db");
	}
	
	function MakeSystemMenu()
	{
		$result = array();
		$parents = array();
		$parent_child = array();
		$parent_child[0] = array();
		
		if($_SESSION['identity']['level'])
		{
			$level = $_SESSION['identity']['level'];
		}else{
			$level = "LEVEL-1";
		}
		
		$mod_menu_permission = new Db_Tables2_BDictMenuPermission();
		$menu_ids = $mod_menu_permission->GetIds($level);
		
		$select = $this->dealers_db->select();
		$select->from("b_dict_menu", "*");
		$select->where("menu_status = ?", 1);
		
		if(!empty($menu_ids))
		{
			$select->where("menu_id IN (?)", $menu_ids);
		}
		
		$select->order("sequence ASC");
		
		$data = $this->dealers_db->fetchAll($select);
		
		if(!empty($data))
		{
			foreach($data as $d)
			{
				if($d['parent_id'])
				{
					if(!$parent_child[$d['parent_id']])
					{
						$parent_child[$d['parent_id']] = array();
					}
					
					$parent_child[$d['parent_id']][$d['menu_id']] = array(
						'menu_id' => $d['menu_id'],
						'menu_name' => $d['menu_name'],
						'url' => $d['url']
					);
					
				}else{
					$parents[] = $d['menu_id'];
					$parent_child[0][$d['menu_id']] = array(
						'menu_id' => $d['menu_id'],
						'menu_name' => $d['menu_name'],
						'url' => $d['url']
					);
				}
			}
			
			if(!empty($parents))
			{
				foreach($parents as $p)
				{
					$result[$p] = $parent_child[0][$p];
					
					if($parent_child[$p])
					{
						$result[$p]['submenu'] = array();
						
						foreach($parent_child[$p] as $pc)
						{
							$result[$p]['submenu'][] = $pc;
						}
					}
				}
			}
		}
		
		return $result;
	}
	
	function MenuDecolation()
	{
		$result = "";
		
		$menu = $this->MakeSystemMenu();
		
		if(!empty($menu))
		{
			foreach($menu as $mval)
			{
				$result .= '<li class="has-sub"><a  href="'.$mval['url'].'">'.$mval['menu_name'].'</a>';
				
				if($mval['submenu'])
				{
					$result .= '<ul>';
					
					foreach($mval['submenu'] as $sval)
					{
						$result .= '<li><a href="'.$sval['url'].'">'.$sval['menu_name'].'</a></li>';
					}
					
					$result .= '</ul>';
				}
					
				$result .= '</li>';
			}
		}
	
		return $result;
		
	}
}